package com.elitel.frame.core.common;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.processors.DefaultValueProcessor;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {

	/**
	 * json转对象 jackson 方式
	 * 
	 * @param <T>
	 * @param t
	 * @param json
	 * @return
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws JsonParseException
	 */
	@SuppressWarnings({ "unchecked", "unused" })
	public static <T> T jsonToObject(Class<T> t, String json)
			throws JsonParseException, JsonMappingException, IOException {
		if (json != null && json != "") {
			ObjectMapper mapper = new ObjectMapper();
			T obj = mapper.readValue(json, t);
			return obj;
		} else
			return null;
	}

	/**
	 * json 转成集合式
	 * 
	 * @param json
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> jsonToListMap(String json)
			throws JsonParseException, JsonMappingException, IOException {
		if (json != null && json != "") {
			ObjectMapper mapper = new ObjectMapper();
			List<Map<String, Object>> list = mapper.readValue(json, List.class);
			return list;
		} else
			return null;
	}

	/**
	 * 对象转json jackson 方式
	 * 
	 * @param obj
	 * @return
	 * @throws JsonProcessingException
	 */
	public static String ObjectToJson(Object obj)
			throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		String jsonStr = mapper.writeValueAsString(obj);
		return jsonStr;
	}

	/**
	 * json转List<?>
	 * 
	 * @param t
	 * @param json
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	@SuppressWarnings({ "unchecked", "unused" })
	public static <T> List<T> jsonToList(Class<T> t, String json)
			throws InstantiationException, IllegalAccessException {
		List<T> resultList = new ArrayList<T>();
		JSON jsonObj = JSONSerializer.toJSON(json);
		if (jsonObj.isEmpty())
			return null;
		if (jsonObj.isArray()) {
			List<?> list = (List<?>) JSONSerializer.toJava(jsonObj);
			for (Object object : list) {
				JSONObject jsonObject = JSONObject.fromObject(object);
				T obj = (T) JSONObject.toBean(jsonObject, t);
				resultList.add(obj);
			}
		} else {
			JSONObject jsonObject = JSONObject.fromObject(jsonObj);
			T obj = (T) JSONObject.toBean(jsonObject, t);
			resultList.add(obj);
		}
		return resultList;
	}

	public static String ToJsonString(Object obj)
			throws JsonProcessingException {
		// 当值为null时，转换后仍为null
		JsonConfig jsonConfig = new JsonConfig();
		DefaultValueProcessor dvp = new DefaultValueProcessor() {

			public Object getDefaultValue(Class type) {
				return null;
			}
		};
		jsonConfig.registerDefaultValueProcessor(BigDecimal.class, dvp);
		jsonConfig.registerDefaultValueProcessor(Integer.class, dvp);
		JSONArray jsonArray = JSONArray.fromObject(obj, jsonConfig);
		String strJson = jsonArray.toString();
		return strJson;
		/*
		 * ObjectMapper objectMapper = new ObjectMapper();
		 * objectMapper.disable(DeserializationFeature
		 * .FAIL_ON_UNKNOWN_PROPERTIES );
		 * objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
		 * String jsonString = objectMapper.writeValueAsString(obj); return
		 * jsonString;
		 */
	}

}
